package com.fs.zise;

import java.util.ArrayList;
import Tools.BitmapTool;
import android.R.integer;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class LaserEmitter {

	private Bitmap laserBitmapUP[];
//	private Bitmap laserBitmapDOWN[];
	private Bitmap laserBitmapRIGHT[];
	private int laserEmitterRow;
	private int laserEmitterCol;
	private int laserEmitterDir = -1;
	private int laser_frameIndex = 0;
	
	GameSurfaceView gameSurfaceView;
	
	
	LaserEmitter(GameSurfaceView gameSurfaceView,int laserEmitterRow, int laserEmitterCol,int laserEmitterDir) {

		this.gameSurfaceView = gameSurfaceView;
		
		this.laserEmitterRow = laserEmitterRow;
		this.laserEmitterCol = laserEmitterCol;
		this.laserEmitterDir = laserEmitterDir;
		System.out.println("LaserEmitter.LaserEmitter()--laserEmitterRow ="+laserEmitterRow);
		System.out.println("LaserEmitter.LaserEmitter()--laserEmitterCol ="+laserEmitterCol);
		
		InitLaserEmitter();
	
	}
	
	

	private void InitLaserEmitter() {
		
		if (laserBitmapUP == null)
			laserBitmapUP = new Bitmap[2];
		
		for (int i = 0; i < laserBitmapUP.length; i++) {
			laserBitmapUP[i] = Bitmap.createBitmap(BitmapTool.getImageFromAssetFile("laserEmitter.png"), 24*i, 0, 24, 20);
		}
		
//		if (laserBitmapDOWN == null)
//			laserBitmapDOWN = new Bitmap[2];
//		
//		for (int i = 0; i < laserBitmapDOWN.length; i++) {
//			laserBitmapDOWN[i] = Bitmap.createBitmap(BitmapTool.getImageFromAssetFile("laserEmitter.png"), 24*(i+4), 0, 24, 20);
//		}
		
		if (laserBitmapRIGHT == null)
			laserBitmapRIGHT = new Bitmap[2];
		
		for (int i = 0; i < laserBitmapRIGHT.length; i++) {
			laserBitmapRIGHT[i] = Bitmap.createBitmap(BitmapTool.getImageFromAssetFile("laserEmitter.png"), 24*(i+2), 0, 24, 20);
		}
	}
	

	public int getLaserEmitterRow() {
		return laserEmitterRow;
	}



	public void setLaserEmitterRow(int laserEmitterRow) {
		this.laserEmitterRow = laserEmitterRow;
	}



	public int getLaserEmitterCol() {
		return laserEmitterCol;
	}



	public void setLaserEmitterCol(int laserEmitterCol) {
		this.laserEmitterCol = laserEmitterCol;
	}



	public int getLaserEmitterDir() {
		return laserEmitterDir;
	}
	
	public void setLaserEmitterDir(int laserEmitterDir) {
		this.laserEmitterDir = laserEmitterDir;
	}

	
	public boolean getLaserEmitter(int row,int  col)
	{
		if(this.laserEmitterRow == row && this.laserEmitterCol ==col )
			return true;
		return false;
	}
	
	public void changeLaserEmitterDirectionAgainstClock()//逆时针改变激光发射器方向
	{
		if(this.laserEmitterDir == GameMap.TOOL_LASER_LEFT )
		{
			setLaserEmitterDir(GameMap.TOOL_LASER_DOWN);
			return ;
		}
		else if(this.laserEmitterDir == GameMap.TOOL_LASER_DOWN)
		{
			setLaserEmitterDir(GameMap.TOOL_LASER_RIGHT);
			return ;
		}
		else if(this.laserEmitterDir == GameMap.TOOL_LASER_RIGHT)
		{
			setLaserEmitterDir(GameMap.TOOL_LASER_UP);
			return ;
		}
		else if(this.laserEmitterDir == GameMap.TOOL_LASER_UP)
		{
			setLaserEmitterDir(GameMap.TOOL_LASER_LEFT);
			return ;
		}
	}
	
	public void drawLaserEmitter(Canvas canvas, Paint paint) {
		
		if(laser_frameIndex<1)
			laser_frameIndex++;
		else
			laser_frameIndex = 0;
		
		int laserEmitter_offx = 12;
		int laserEmitter_offy = 14;
		switch (laserEmitterDir) {
		case GameMap.TOOL_LASER_UP:
			canvas.drawBitmap(laserBitmapUP[laser_frameIndex], laserEmitterCol*48+laserEmitter_offx, laserEmitterRow*48+laserEmitter_offy, paint);
			break;
		case GameMap.TOOL_LASER_DOWN:
			BitmapTool.drawRegoin(canvas, paint, laserBitmapUP[laser_frameIndex], 2, 
					laserEmitterCol*48+laserEmitter_offx, laserEmitterRow*48+laserEmitter_offy, 0, 0, 24, 20);
			break;
		case GameMap.TOOL_LASER_RIGHT:
			canvas.drawBitmap(laserBitmapRIGHT[laser_frameIndex], laserEmitterCol*48+laserEmitter_offx, laserEmitterRow*48+laserEmitter_offy, paint);
			break;
		case GameMap.TOOL_LASER_LEFT:
			BitmapTool.drawRegoin(canvas, paint, laserBitmapRIGHT[laser_frameIndex], 1, 
					laserEmitterCol*48+laserEmitter_offx, laserEmitterRow*48+laserEmitter_offy, 0, 0, 24, 20);
			break;
		}
	}
	
	class Spot
	{
		int r;
		int c;
		
		Spot(int r,int c)
		{
			this.r = r;
			this.c = c;
		}
	}
	
	
	int oldr;
	int oldc;
	int current_R;
	int current_C;
	private ArrayList<Spot> arrayList = new ArrayList<Spot>();
	
	private boolean getPointer(int r,int c)
	{
//		System.out.println("LaserEmitter.getPointer()[r][c] = "+r+"--"+c+"--"+GameMap.zise0_map0_one[GameMap.getSingleInstance().getLevel()][r][c]);
		if(GameMap.getSingleInstance().zise0_map0_one[r][c] == 38)
		{
			getArrayList().add(new Spot(r, c));
			
			for (int i = 0; i < gameSurfaceView.mMirrors.size(); i++) {
//				System.out.println("SSSSSS = "+gameSurfaceView.mMirrors.get(i).getMirror_Col());
				if(gameSurfaceView.mMirrors.get(i).getMirror_Col() == c
				&& gameSurfaceView.mMirrors.get(i).getMirror_Row() == r)
				{
					System.out.println("LaserEmitter.getPointer()111111111111");
						if(getGuangDirAccordingToMirror(gameSurfaceView.mMirrors.get(i).getMirrorDir()) == RIGHT) //you
						{
							oldr = current_R;
							oldc = current_C;
							current_C ++;
						}
						else if(getGuangDirAccordingToMirror(gameSurfaceView.mMirrors.get(i).getMirrorDir()) == LEFT)//zuo
						{
							oldr = current_R;
							oldc = current_C;
							current_C --;
						}
						else if(getGuangDirAccordingToMirror(gameSurfaceView.mMirrors.get(i).getMirrorDir()) == UP)//shang
						{
							oldr = current_R;
							oldc = current_C;
							current_R --;
						}
						else if(getGuangDirAccordingToMirror(gameSurfaceView.mMirrors.get(i).getMirrorDir()) == DOWN)//xia
						{
							oldr = current_R;
							oldc = current_C;
							current_R ++;
						}
						break;
				}
			}
			System.out.println("AAA");
			return false;
		}
		else if(GameMap.getSingleInstance().zise0_map0_one[r][c] == 1)
		{
			oldr = current_R;
			oldc = current_C;
			System.out.println("BBB");
			return true;
		}
		else if(GameMap.getSingleInstance().zise0_map0_one[r][c] == 2)
		{
			
			oldr = current_R;
			oldc = current_C;
			System.out.println("BBB");
			return true;
		}
		else {
			
//			if(isOutOfMapBound())
//				return true;
			
			getArrayList().add(new Spot(r, c));
			
			if(GuangYuanDir() == UP) //shang
			{
				oldr = current_R;
				oldc = current_C;
				
				current_R--;
			}
			
			else if(GuangYuanDir() == DOWN) //xia
			{
				oldr = current_R;
				oldc = current_C;
				
				current_R++;
			}
			
			else if(GuangYuanDir() == LEFT) //zuo
			{
				oldr = current_R;
				oldc = current_C;
				
				current_C--;
			}
			
			else if(GuangYuanDir() == RIGHT) //you
			{
				oldr = current_R;
				oldc = current_C;
				
				current_C++;
			}
			
			if (current_C<0||current_R<0||current_C>GameMap.getSingleInstance().zise0_map0_one[0].length-1||current_R>GameMap.getSingleInstance().zise0_map0_one.length-1) 
				return true;
			
			return false;
		}
	}
	
	private boolean isOutOfMapBound()
	{
		if(laserEmitterDir == GameMap.TOOL_LASER_LEFT  && laserEmitterCol == 0)
			return true;
		if( laserEmitterDir == GameMap.TOOL_LASER_RIGHT && laserEmitterCol == GameMap.getSingleInstance().zise0_map0_one[0].length -1)
			return true;
		if( laserEmitterDir == GameMap.TOOL_LASER_UP && laserEmitterRow == 0)
			return true;
		if( laserEmitterDir == GameMap.TOOL_LASER_DOWN && laserEmitterRow == GameMap.getSingleInstance().zise0_map0_one.length - 1)
			return true;
		
		return false;
	}
	
	private int GuangYuanDir()
	{
		if(oldr < current_R)
			return DOWN; //
		if(oldr > current_R)
			return UP; //
		if(oldc < current_C)
			return RIGHT; //
		if(oldc > current_C)
			return LEFT; //
		
		return -1;
		
	}
	
	
	private final int RIGHT = 100;
	private final int LEFT = 101;
	private final int UP = 102;
	private final int DOWN = 103;
	
	private int getGuangDirAccordingToMirror(int mirrorDirection)
	{
		if(mirrorDirection == GameMap.MIRROR_DIRECTION_XIE_1TH)
		{
		if(oldr < current_R)
			return RIGHT; //
		if(oldr > current_R)
			return LEFT; //
		if(oldc < current_C)
			return DOWN; //
		if(oldc > current_C)
			return UP; //
		}
		else if(mirrorDirection == GameMap.MIRROR_DIRECTION_XIE_2TH)
		{
			if(oldr < current_R)
				return LEFT; //
			if(oldr > current_R)
				return RIGHT; //
			if(oldc < current_C)
				return UP; //
			if(oldc > current_C)
				return DOWN; //
		}
			return -1;
	}
	
	
	
	public LaserEmitter light()
	{
		getArrayList().clear();
			oldc = laserEmitterCol;
			oldr = laserEmitterRow;
			
			if (!isOutOfMapBound()) {
				
				if( laserEmitterDir == GameMap.TOOL_LASER_LEFT )
				{
//					if(oldc == 0)
//						return null;
					
					current_C = oldc - 1;
					
					current_R = oldr;
				}
				
				if( laserEmitterDir == GameMap.TOOL_LASER_RIGHT )
				{
//					if(oldc == GameMap.zise0_map0_one[0].length-1)
//						return null;
					
					current_C = oldc + 1;
					current_R = oldr;
					
					System.out.println("LaserEmitter.light()-------------------right");
				}
				
				if( laserEmitterDir == GameMap.TOOL_LASER_UP )
				{
//					if(oldr ==0)
//						return null;
					current_R = oldr - 1;
					current_C = oldc;
					
				}
				if( laserEmitterDir == GameMap.TOOL_LASER_DOWN )
				{
//					if(oldr ==GameMap.zise0_map0_one.length-1)
//						return null;
					current_R = oldr + 1;
					current_C = oldc;
					
				}
				
				
				while (!getPointer(current_R, current_C)) {
				}
			}
			return this;
	}
		
		public void drawLaser2(Canvas canvas, Paint paint)
		{
			paint.setColor(Color.RED);
//			System.out.println("LaserEmitter.drawLaser2()-->"+arrayList.size());
//			for (int i = 0; i < arrayList.size(); i++) {
//				System.out.println("arrayListR =" + "[" + i + "]" +" "+arrayList.get(i).r +"行 ");
//				System.out.println("arrayListC =" + "[" + i + "]" +" "+arrayList.get(i).c +"列 ");
//			}
			
			if(!getArrayList().isEmpty() && getArrayList().size()>=2)
			{
				int lastDotPathDirection = -1; 
					if(getArrayList().get(getArrayList().size()-1).r == getArrayList().get(getArrayList().size()-2).r)
					{
						lastDotPathDirection = 10;//最后一段和最后第二段是同行
					}
					else if(getArrayList().get(getArrayList().size()-1).c == getArrayList().get(getArrayList().size()-2).c)
					{
						lastDotPathDirection = 20;//最后一段和最后第二段是同列
					}
				int offx = 24;
				int offy = 24;
				if(laserEmitterDir == GameMap.TOOL_LASER_LEFT)
				{
					canvas.drawLine((float) laserEmitterCol * 48 + 12,laserEmitterRow*48 + offy,
							(float)(laserEmitterCol-1) * 48 + offx ,laserEmitterRow*48 + offy,paint);
					
				}
				if(laserEmitterDir == GameMap.TOOL_LASER_RIGHT)
				{
					canvas.drawLine((float) (laserEmitterCol+1) * 48 - 12 ,laserEmitterRow*48 + offy,
							(float)(laserEmitterCol+1) * 48 + offx ,laserEmitterRow*48 + offy,paint);
					
				}
				else if(laserEmitterDir == GameMap.TOOL_LASER_UP)
				{
					canvas.drawLine((float) laserEmitterCol * 48 +offx ,laserEmitterRow*48 + 12,
							(float)laserEmitterCol * 48 + offx ,(laserEmitterRow-1)*48 + offx,paint);
				}
				else if(laserEmitterDir == GameMap.TOOL_LASER_DOWN)
				{
					canvas.drawLine((float) laserEmitterCol * 48 +offx ,(laserEmitterRow+1)*48 - 12,
							(float)laserEmitterCol * 48 + offx ,(laserEmitterRow+1)*48 + offx,paint);
				}
				
				
				for (int i = 0; i < getArrayList().size(); i++) {
					 if(i<getArrayList().size()-1)
					{
						 canvas.drawLine((float)getArrayList().get(i).c * 48 + offx,(float)getArrayList().get(i).r*48 + offy,
									(float)getArrayList().get(i+1).c * 48 + offx ,(float)getArrayList().get(i+1).r*48 + offy,paint);
					}
					else
					{
						if(lastDotPathDirection == 20)//最后一段和最后第二段是同列
						{
							canvas.drawLine( getArrayList().get(getArrayList().size()-1).c * 48+ offx,getArrayList().get(getArrayList().size()-1).r * 48,
									getArrayList().get(getArrayList().size()-1).c* 48+ offx,(getArrayList().get(getArrayList().size()-1).r+1)* 48 ,  paint);
						}
						
						else if(lastDotPathDirection == 10)//最后一段和最后第二段是同行
						{
							canvas.drawLine( getArrayList().get(getArrayList().size()-1).c* 48,getArrayList().get(getArrayList().size()-1).r* 48 + offy,
									(getArrayList().get(getArrayList().size()-1).c+1)* 48 ,getArrayList().get(getArrayList().size()-1).r* 48 + offy,  paint);
						
						}
					}	
				}
			}
			else if(!getArrayList().isEmpty() && getArrayList().size() == 1)//激光路径下如果只有1段的话
			{
				int offx = 24;
				int offy = 24;
				if(laserEmitterDir == GameMap.TOOL_LASER_LEFT)
				{
					canvas.drawLine((float) laserEmitterCol * 48 + 12,laserEmitterRow*48 + offy,
							(float)(laserEmitterCol-1) * 48  ,laserEmitterRow*48 + offy,paint);
					
				}
				if(laserEmitterDir == GameMap.TOOL_LASER_RIGHT)
				{
					canvas.drawLine((float) (laserEmitterCol+1) * 48 - 12 ,laserEmitterRow*48 + offy,
							(float)(laserEmitterCol+1) * 48 + offx*2 ,laserEmitterRow*48 + offy,paint);
					
				}
				else if(laserEmitterDir == GameMap.TOOL_LASER_UP)
				{
					canvas.drawLine((float) laserEmitterCol * 48 +offx ,laserEmitterRow*48 + 12,
							(float)laserEmitterCol * 48 + offx ,(laserEmitterRow-1)*48 ,paint);
				}
				else if(laserEmitterDir == GameMap.TOOL_LASER_DOWN)
				{
					canvas.drawLine((float) laserEmitterCol * 48 +offx ,(laserEmitterRow+1)*48 - 12,
							(float)laserEmitterCol * 48 + offx ,(laserEmitterRow+1)*48 + offx*2,paint);
				}
				
			}
		}

		public void setArrayList(ArrayList<Spot> arrayList) {
			this.arrayList = arrayList;
		}



		public ArrayList<Spot> getArrayList() {
			return arrayList;
		}
		
		//根据 传入的元素行列值  判断该元素是否处于 激光器所产生的路线上
		public boolean getElementValueInLightPath(int row,int col)
		{
			for (int i = 0; i < arrayList.size(); i++) {
				if(arrayList.get(i).r == row&&arrayList.get(i).c == col)
				{
					return true;
				}
			}
			return false;
		}
}
